package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entiy.dto.Goods;
import com.qf.entiy.dto.GoodsSku;
import com.qf.entiy.po.LoginMerchant;
import com.qf.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsService extends ServiceImpl<GoodsMapper, Goods> {

    @Autowired
    private GoodsSkuService goodsSkuService;
    //获取商品类型
    @Transactional(readOnly = true)
    public Object getByTypeId(Long goodsTypeId) {

        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (goodsTypeId>0){
            wrapper.lambda().eq(Goods::getGoodsTypeId,goodsTypeId);
        }
        wrapper.lambda().orderByAsc(Goods::getOrderNum);
        return this.list(wrapper);
    }
    //上架
    @Transactional
    public void up(Long goodsId){
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Goods::getId,goodsId).set(Goods::getState,Goods.State.UP.getCode());
        this.update(wrapper);
    }
    //下架
    @Transactional
    public void down(Long goodsId){
        UpdateWrapper<Goods> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Goods::getId, goodsId).set(Goods::getState, Goods.State.DOWN.getCode());
        this.update(wrapper);
    }
    //根据登录用户id查询上传的商品
    @Transactional(readOnly = true)
    public Object getByMerchant(Long loginMerchantId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getMerchantUserId,loginMerchantId);
        return this.list(wrapper);
    }
    //删除
    @Transactional
    public void removeGoods(Long id) {
        this.removeById(id);
        goodsSkuService.removeByGoodsId(id);
    }
    //新增
    @Transactional
    public void saveGoods(Goods goods, LoginMerchant loginMerchant) {
        if (goods.getId()!=null){
            goodsSkuService.removeByGoodsId(goods.getId());
        }
        goods.setMerchantUserId(loginMerchant.getId());
        goods.setState(Goods.State.PENDING.getCode());
        this.saveOrUpdate(goods);

        String [] titles = goods.getSkuTitle().split("\\|");
        String [] costs = goods.getSkuCost().split("\\|");
        String [] prices = goods.getSkuPrice().split("\\|");
        String [] pmoneys = goods.getSkuPmoney().split("\\|");

        List<GoodsSku> goodsSkuList = new ArrayList<>(titles.length);
        int orderNum = 1;
        for (int i = 0; i < titles.length; i++) {
            GoodsSku goodsSku = new GoodsSku();
            goodsSku.setGoodId(goods.getId());
            goodsSku.setTitle(titles[i]);
            goodsSku.setCost(titles[i]);
            goodsSku.setPrice(prices[i]);
            goodsSku.setPmoney(pmoneys[i]);
            goodsSku.setOrderNo(new Long(orderNum));
            goodsSkuList.add(goodsSku);
            orderNum++;
        }
            goodsSkuService.saveBatch(goodsSkuList,goodsSkuList.size());
    }

    @Transactional(readOnly = true)
    public Object getGoodsList(LoginMerchant loginMerchant) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Goods::getState,Goods.State.UP.getCode())
                        .eq(Goods::getMerchantUserId,loginMerchant.getId());
        return this.list(wrapper);
    }
}
